package com.ebelter.bodyfatscale.ui.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.TextView;

import com.chipsea.healthscale.CsAlgoBuilderEx;
import com.ebelter.bodyfatscale.ApplicationHelper;
import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.ui.view.MyDashBoardView2;
import com.ebelter.bodyfatscale.ui.view.MyPswEd;
import com.ebelter.bodyfatscale.ui.view.dialogs.OfflineTextTipDialog;
import com.ebelter.bodyfatscale.util.NumUtils;
import com.ebelter.bodyfatscale.util.ScaleBaseUtils;
import com.ebelter.bodyfatscale.util.StringUtils;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.btlibrary.util.ULog;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CeshiActivity extends BaseActivity {
    private static final String TAG = "CeshiActivity";


    private TextView ceshi_tv;
    private MyDashBoardView2 myDashBoardView;
    private boolean isKGModel;

   // EditText ceshi_ed;
    MyPswEd myPswEd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Constants.isKgUnit = true;
        myDashBoardView=findViewById(R.id.ceshi_MyDashBoardView);
        //ceshi_ed=findViewById(R.id.ceshi_ed);
        myPswEd=findViewById(R.id.mypsw_ed);

        ceshi_tv=findViewById(R.id.ceshi_tv);
        ULog.i(TAG,"默认字体大小 = "+ceshi_tv.getTextSize());
    }

    @Override
    protected int loadLayoutById() {
        return R.layout.content_ceshi;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {

    }

    //
    private void initDatas(int count){



    }

    public void btClickA(View view) {

//        WeighTipDialog weighTipDialog=new WeighTipDialog(this);
//        weighTipDialog.show();

      //  startActivity(BindDeviceActivity.class);
//        myDashBoardView.setDisplayValue(70);
//        myDashBoardView.show();

      //  startActivity(AddUserInfoActivity.class);


       // /storage/emulated/0/download/purescale_v1.2.5.apk

        File file=new File("/storage/emulated/0/download/purescale_v1.2.5.apk");
        if (!file.exists()){
            ToastUtil.show("文件不存在");
            return;
        }else {
            ToastUtil.show("文件存在。。。");
        }

        //UpdateService.installApp(getApplicationContext(),"/storage/emulated/0/download/purescale_v1.2.5.apk","com.ebelter.bodyfatscale4.fileprovider");


    }

    public static String formatDateStampString2(long when) {
        int format_flags = DateUtils.FORMAT_SHOW_TIME| DateUtils.FORMAT_NO_MIDNIGHT|DateUtils.FORMAT_NO_NOON;
        return DateUtils.formatDateTime(ApplicationHelper.mApplicationContecx, when, format_flags);
    }


    private static String unitKg = StringUtils.getResStr(R.string.kg);
    private static String unitLb = StringUtils.getResStr(R.string.lb);
    public void btClickB(View view) {
//        float sp18=getResources().getDimension(R.dimen.sp18);
//        ULog.i(TAG,"sp18="+sp18);
//        ceshi_tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,sp18);
        OfflineTextTipDialog textTipDialog=new OfflineTextTipDialog(this, StringUtils.getResStr(R.string.Offline_measurements_awaiting_confirmation));
        String valueTip = StringUtils.getResStr(R.string.offline_data_value_tip);

        String weight= NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(66.6f),1)+"";
        if (Constants.isKgUnit){
            weight= weight+unitKg;
        }else {
            weight=ScaleBaseUtils.unitDisplay2(weight,unitLb);
        }


        String time=ScaleBaseUtils.timeFatmat("2018-06-22 08:33:22",3);
        textTipDialog.setListener(new OfflineTextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }
            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();


            }
        });
        textTipDialog.show();
        textTipDialog.setTip2Str(String.format(valueTip,weight,time));



    }
    public void btClickC(View view) {

        ULog.i(TAG,"------时间格式化");
        Locale locale=getResources().getConfiguration().locale;
        ULog.i(TAG,"------当前环境：locale.getCountry()="+locale.getCountry()+"--locale.getDisplayLanguage()="+locale.getDisplayLanguage()+"---locale.getDisplayName()="+locale.getDisplayName());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",locale);
        Date now=new Date();
        System.out.println(sdf .format(now));

        ULog.i(TAG,"------时间格式化--"+sdf .format(now));

//        ------时间格式化
//       ------当前环境：locale.getCountry()=CN--locale.getDisplayLanguage()=中文---locale.getDisplayName()=中文 (简体中文,中国)
//       ------时间格式化--2018-05-23 11:14:58

    }
    public void btClickD(View view) {
//        //男	普通	170	24	469	73	23
//        mesure(1,0,170,24,73,23,469);
//
//        //男	运动员	170	24	437	73	14.6
//        mesure(1,1,170,24,73,14.6f,437);
//
//        //女	普通	170	35	467	69	32.8
//        mesure(0,0,170,35,69,32.8f,467);
//
//        //女	运动员	170	24	440	73	30.1
//        mesure(0,1,170,24,73,30.1f,440);




    }
    //0女1男
    //1为运动员，0为普通用户
    private void mesure(int sex,int mode,int height,int age,float weight,float fat,float resistance) {
        CsAlgoBuilderEx mBuilderEx = new CsAlgoBuilderEx();
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("测试信息--");
        if (sex==0){
            stringBuilder.append("--性别=女");
        }else if (sex==1){
            stringBuilder.append("--性别=男");
        }
        if (mode==1){
            stringBuilder.append("--运动类型=运动员");
        }else if (mode==0){
            stringBuilder.append("--运动类型=普通");
        }
        stringBuilder.append("---身高=").append(height);
        stringBuilder.append("---年龄=").append(age);
        stringBuilder.append("---体重=").append(weight);
        stringBuilder.append("---脂肪=").append(fat);
        stringBuilder.append("---阻抗=").append(resistance);

        ULog.i(TAG,stringBuilder.toString());
//        性别	运动模式	身高CM	年龄 岁	加密后电阻（欧姆）	体重Kg	脂肪%
//        男	普通	170	24	469	73	23
        mBuilderEx.setUserInfo(height, weight, (byte) sex, age, resistance);
        //设置用户角色，1为运动员，0为普通用户
        mBuilderEx.setMode(mode);
        float wr = mBuilderEx.getTFR();
        float bmr = mBuilderEx.getBMR();
        float vf = mBuilderEx.getVFR();
        float mv = mBuilderEx.getSMM();
        float  sm = mBuilderEx.getSLM();
        float bv = mBuilderEx.getMSW();
        float ba = (int) mBuilderEx.getBodyAge();
        float protein = mBuilderEx.getPM();
        protein = dbzKgToPer(protein, weight);
        float score = mBuilderEx.getScore();
        float bmi=getBmi(weight, height);


        ULog.i(TAG,"---测试结果信息 水分="+wr+"--bmr="+bmr+"--内脂="+vf+"--骨骼肌="+mv+"--肌肉量="+sm+
                "--骨盐量="+bv+"--身体年龄="+ba+"--蛋白质="+protein+"--身体得分="+score+"--bmi="+bmi);
        ULog.i(TAG,"\n");
    }

    private float dbzKgToPer(float dbz, float weight) {
        float preF = dbz / weight * 100;
        preF = Math.round(preF * 10) / 10.0f;
        preF = Math.min(preF, 100.0f);
        return preF;
    }
    private float getBmi(float weight, float userHeight) {
        float bmi = weight / (userHeight * userHeight / 10000);
        return Math.round(bmi * 10) / 10.0f;
    }
}
